package com.cw.service.impl;

import com.cw.constant.Constants;
import com.cw.entity.RegisterRecord;
import com.cw.entity.SysUser;
import com.cw.mapper2.RegisterRecordMapper;
import com.cw.service.IRegisterRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.service.ISysUserService;
import com.cw.util.HttpUtil;
import com.cw.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 登记记录表 服务实现类
 * </p>
 *
 * @author xnd
 * @since 2021-09-07
 */
@Service
public class RegisterRecordServiceImpl extends ServiceImpl<RegisterRecordMapper, RegisterRecord> implements IRegisterRecordService {

    @Autowired
    private ISysUserService iSysUserService;

    @Override
    public boolean insertRegisterRecord(String preId, String state) {
        RegisterRecord registerRecord = new RegisterRecord();
        registerRecord.setPreId(preId);
        registerRecord.setState(state);
        registerRecord.setId(UUIDUtil.uuid());
        registerRecord.setCrateTime(LocalDateTime.now());

        String userId = HttpUtil.getRequestHeaderInfo(Constants.USER_ID);
        List<SysUser> sysUsers = iSysUserService.getSysUser(userId, null);
        SysUser sysUser = sysUsers.get(0);
        String realname = sysUser.getRealname();
        String username = sysUser.getUsername();
        registerRecord.setExecutionName(realname);
        registerRecord.setExecutionEmployee(username);
        return baseMapper.insertRegisterRecord(registerRecord) > 0;
    }

    @Override
    public boolean delectRegisterRecord(String preId) {
        return baseMapper.deleteReginterRecord(preId)>0;
    }
    @Override
    public boolean selectReginterRecord(String preId){
        return baseMapper.selectReginterRecord(preId)>0;
    }
}
